//==============================================================================
//
//               Copyright (C) 2004  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2004-07-25 08:53:32 $ $Revision: 1.2 $
//
//==============================================================================

#include <stdlib.h>

#include "ofc/config.h"

#include "ofc/DURL.h"

#include "DInc.h"
#include "DTest.h"

//-Misc------------------------------------------------------------------------

void DURL_test()
{
  DURL  *url1 = [DURL new];
  DURL  *url2 = [DURL alloc];
  DText *url3 = nil;

  char   url4[] = "   ";
  char   url5[] = "  ftp://user:password@www.host.org:23/index.html  ";
  char  *url6;
  
  STARTTEST();

  // Full test
  [url2 init :url5];
  
  TEST(strcmp([url2 host],     "www.host.org") == 0);
  TEST(strcmp([url2 scheme],   "ftp"         ) == 0);
  TEST(strcmp([url2 user],     "user"        ) == 0);
  TEST(strcmp([url2 password], "password"    ) == 0);
  TEST(strcmp([url2 path],     "/index.html" ) == 0);
  
  TEST([url2 port] == 23);
  
  url3 = [url2 url];
  TEST(strcmp([url3 cstring], "ftp://user:password@www.host.org:23/index.html") == 0); 
  
  [url3 free];
  
  // Only server
  [url2 url :"www.server.net"];
  
  TEST(strcmp([url2 host], "www.server.net") == 0);
  TEST(strcmp([url2 scheme], "") == 0);
  TEST([url2 user] == NULL);
  TEST([url2 password] == NULL);
  TEST(strcmp([url2 path],"") == 0);
  
  // Only server and port
  [url2 url :"www.server.net:12"];
  
  TEST(strcmp([url2 host], "www.server.net") == 0);
  TEST(strcmp([url2 scheme], "") == 0);
  TEST([url2 user] == NULL);
  TEST([url2 password] == NULL);
  TEST(strcmp([url2 path],"") == 0);
  TEST([url2 port] == 12);
  
  url3 = [url2 url];
  TEST(strcmp([url3 cstring], "//www.server.net:12") == 0);
  [url3 free];
  
  // Only path
  [url2 url :"/home/report.html"];
  
  TEST(strcmp([url2 host], "") == 0); 
  TEST(strcmp([url2 scheme], "") == 0);
  TEST([url2 user] == NULL);
  TEST([url2 password] == NULL);
  TEST(strcmp([url2 path],"/home/report.html") == 0);
  
  // Empty user name, no password
  [url2 url :"ftp://@host.tv"];

  TEST(strcmp([url2 host], "host.tv") == 0); 
  TEST(strcmp([url2 scheme], "ftp") == 0);
  TEST(strcmp([url2 user], "") == 0);
  TEST([url2 password] == NULL);
  TEST(strcmp([url2 path], "") == 0);
  TEST([url2 port] == 0);

  // User with empty password
  [url2 url :"file://me:@ftp.client.nl"];

  TEST(strcmp([url2 host], "ftp.client.nl") == 0); 
  TEST(strcmp([url2 scheme], "file") == 0);
  TEST(strcmp([url2 user], "me") == 0);
  TEST(strcmp([url2 password], "") == 0);
  TEST(strcmp([url2 path], "") == 0);
  TEST([url2 port] == 0);

  // With reference
  [url1 url :"/home.html" :url2];
  
  TEST(strcmp([url1 host], "ftp.client.nl") == 0); 
  TEST(strcmp([url1 scheme], "file") == 0);
  TEST(strcmp([url1 user], "me") == 0);
  TEST(strcmp([url1 password], "") == 0);
  TEST(strcmp([url1 path], "/home.html") == 0);
  TEST([url1 port] == 0);
  
  //printf("%s\n", [[url1 url] cstring]);

  // fromString tests
  url6 = url4;
  TEST([url1 fromString :&url6] == ENODATA);
  TEST(*url6 == EOS);
  
  url6 = url5;
  TEST([url1 fromString :&url6] == 0);
  TEST(*url6 == ' ');
  
  [url2 free];
  [url1 free];
  
  STOPTEST();
}

